package cn.itcast.interceptor;

import cn.itcast.common.R;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Slf4j
public class CheckLoginInterceptor implements HandlerInterceptor {
    private static final AntPathMatcher    PATH_MATCHER =new AntPathMatcher();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Long login_id = (Long) request.getSession().getAttribute("LOGIN_ID");
        if (login_id !=null){
            log.info("id为{}的用户已登录",login_id);
            return true;
        }
        log.error("拦截到的请求: {}",request.getRequestURI());
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        String json = JSON.toJSONString(R.error("NOTLOGIN"));
        out.println(json);
        return false;
    }
}
